﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Sua_XoaPhuKienDT : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)// ko co phan nay ko update dc
        {
            LoadLoaiPhuKien();
            LoadGridview();
        }
    }
    protected void LoadLoaiPhuKien()// Load dropdown LoaiDienThoai
    {
        dt = obj.loadDataTable("Select * from LoaiPhuKien");
        drploaiphukien.DataTextField = "TenLoaiPK";
        drploaiphukien.DataValueField = "MaLoaiPK";
        drploaiphukien.DataSource = dt;
        drploaiphukien.DataBind();
    }
    protected void LoadGridview()
    {
        dt = obj.loadDataTable("Select * from PhuKienDienThoai where MaLoaiPK='" + drploaiphukien.SelectedValue.ToString() + "'");
        GridView3.DataSource = dt;
        GridView3.DataBind();
    }
  

    protected void drploaiDT_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadGridview();// Load lai gridview
    }
    protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView3.PageIndex = e.NewPageIndex;
        LoadGridview();
    }
    protected void GridView3_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView3.EditIndex = -1;
        LoadGridview();
    }
    protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int maphukien = Convert.ToInt32(GridView3.DataKeys[e.RowIndex].Value);
        obj.XoaPhuKienDT(maphukien);
        Response.Write("<script language='javascript'> alert('Xoá thành công !' ) </script>");
        LoadGridview();
    }
    protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView3.EditIndex = e.NewEditIndex;
        LoadGridview();
    }

    private void luuhinh(FileUpload a)// Luu Hinh
    {
        string strfilename;


        if (a.FileName != "")
        {
            strfilename = Server.MapPath("Upload\\" + a.FileName);
            a.PostedFile.SaveAs(strfilename);
        }
        else
        {



        }
    }
    protected void GridView3_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int maphukien = Convert.ToInt32(GridView3.DataKeys[e.RowIndex].Value);
        string tenphukien = (GridView3.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        string gia = (GridView3.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
        TextBox txtthongtin = GridView3.Rows[GridView3.EditIndex].FindControl("txtthongtin") as TextBox; ;
        string thongtin = txtthongtin.Text;
        string baohanh = (GridView3.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text;
        //string thongtin = (GridView3.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        FileUpload fileUpLoad = GridView3.Rows[GridView3.EditIndex].FindControl("FileUpload1") as FileUpload; ;// Tim fileupload trong edit item cua gridview
        string hinhanh = fileUpLoad.FileName.ToString();
        if (fileUpLoad.FileName != "")
        {
            luuhinh(fileUpLoad);//goi ham luu hinh add ten file upload vao
            obj.UpdatePhuKienDT(tenphukien, gia, baohanh, thongtin, hinhanh, maphukien);
            GridView3.EditIndex = -1;
            LoadGridview();
        }
        else // Neu khong co hinh thi bo update hinh
        {
            obj.UpdatePhuKienDTNeuHinhNull(tenphukien, gia, baohanh, thongtin, maphukien);// goi ham update bo wa truong hinh
            GridView3.EditIndex = -1;
            LoadGridview();
        }
    }
}
